package com.ramos.kuaixiao.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.ramos.kuaixiao.entity.PriceType;

import java.util.List;

//价格套本地数据
public class PriceTypeDB {
    private static String TAG = "PriceTypeDB";
    private static String TABLE_NAME = "PriceType";
    public static final String CREATE_TABLE_SQL;
    public static final String DROP_USERINFO;

    static {
        CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + " (" + "did" + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
                + "dealerId" + " CHAR(30)," + "priceTypeId" + " CHAR(30)," + "priceTypeName" + " CHAR(30)," + "isDefault" + " INTEGER,"
                + "status" + " CHAR(30),"+ "modifyTime" + " Long);";
        DROP_USERINFO = "DROP TABLE IF EXISTS " + TABLE_NAME;
    }

    public int addAllPriceType(List<PriceType> customer) {
        //deleteAll();
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM PriceType");
        String sql = "insert into PriceType(dealerId,priceTypeId,priceTypeName,isDefault,status,modifyTime) values(?,?,?,?,?,?)";
        db.beginTransaction();
        Long a = System.currentTimeMillis();
        SQLiteStatement stmt = db.compileStatement(sql);
        for (int i = 0; i < customer.size(); i++) {
            stmt.bindString(1, customer.get(i).getDealerId() + "");
            stmt.bindString(2, customer.get(i).getPriceTypeId() + "");
            stmt.bindString(3, customer.get(i).getPriceTypeName() + "");
            stmt.bindString(4, customer.get(i).getIsDefault() + "");
            stmt.bindString(5, customer.get(i).getStatus() + "");
            stmt.bindString(6, customer.get(i).getModifyTime() + "");
            stmt.execute();
            stmt.clearBindings();
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        Long b = System.currentTimeMillis();
        Log.i(TAG, String.valueOf(b - a));
        return 0;
    }

    public int updateAllPriceType(List<PriceType> customer) {
        SQLiteDatabase db = DBHelper.open();
        db.beginTransaction();
        Long a = System.currentTimeMillis();
        Log.d(TAG, "updateAllKeywords:" + customer.size());
        for (int i = 0; i < customer.size(); i++) {
            PriceType keywords = customer.get(i);
            if (hasPriceType(keywords.getPriceTypeId() + "")) {
                ContentValues cv = new ContentValues();
                cv.put("dealerId", keywords.getDealerId() + "");
                cv.put("priceTypeName", keywords.getPriceTypeName() + "");
                cv.put("isDefault", keywords.getIsDefault() + "");
                cv.put("status", keywords.getStatus() + "");
                cv.put("modifyTime", keywords.getModifyTime() + "");
                int n = db.update("PriceType", cv, "priceTypeId=?", new String[]{keywords.getPriceTypeId() + ""});
            } else {
                ContentValues cv = new ContentValues();
                cv.put("dealerId", keywords.getDealerId() + "");
                cv.put("priceTypeId", keywords.getPriceTypeId() + "");
                cv.put("priceTypeName", keywords.getPriceTypeName() + "");
                cv.put("isDefault", keywords.getIsDefault() + "");
                cv.put("status", keywords.getStatus() + "");
                cv.put("modifyTime", keywords.getModifyTime() + "");
                long n = db.insert("PriceType", null, cv); //如果有记录，则更新，并返回1，否则返回0
            }
            // 生成的sql是 INSERT INTRO OR REPLACE INTO 这样的 (如果存在就替换存在的字段值. 存在的判断标准是主键冲突, 这里的主键是userId). 下面会介绍这个地方的方法
            //db.insertWithOnConflict("StoreNum",null, cv, SQLiteDatabase.CONFLICT_REPLACE) ; //如果有记录，则更新，并返回1，否则返回0
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        Long b = System.currentTimeMillis();
        Log.i(TAG, String.valueOf(b - a));
        return 0;
    }

    public boolean hasPriceType(String pkey) {
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select * from PriceType where priceTypeId = ?", new String[]{pkey});
        if (cursor.getCount() > 0) {
            cursor.close();
            Log.d(TAG, "hasKeywords true");
            return true;
        } else {
            Log.d(TAG, "hasKeywords false");
            cursor.close();
            return false;
        }
    }

    public long getMaxModifyTime() {
        long param = 0;
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select max(modifyTime)from PriceType ", new String[]{});
        Log.d(TAG, "getMaxModifyTime:getCount" + cursor.getCount());
        if (cursor.moveToFirst()) {
            param = cursor.getLong(0);
        }
        cursor.close();
        Log.d(TAG, "getMaxModifyTime:param" + param);
        return param;
    }

    public void deleteAll() {
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM PriceType");
    }

    public String getDefault() {
        String isDefault ="";
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select * from PriceType where isDefault=?", new String[]{String.valueOf(1)});
        Log.d(TAG, "cursor:" + cursor.getCount());
        if (cursor.moveToFirst()) {
            isDefault = cursor.getString(2);
        }
        cursor.close();
        //db.close();
        return isDefault;
    }

    public boolean hasData() {
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select count(*)from PriceType", null);
        cursor.moveToFirst();
        Long count = cursor.getLong(0);
        if (count > 0) {
            cursor.close();
            Log.d(TAG, "hasPriceType true");
            return true;
        } else {
            Log.d(TAG, "hasPriceType false");
            cursor.close();
            return false;
        }
    }
}
